package com.bitcola.security.admin.biz;

import com.alibaba.fastjson.JSON;
import com.bitcola.security.admin.entity.SysUserEntity;
import com.bitcola.security.admin.feign.IActivityFeign;
import com.bitcola.security.admin.mapper.ColaKycMapper;
import com.bitcola.security.admin.mapper.ColaUserMapper;
import com.bitcola.security.admin.mapper.UserMapper;
import com.bitcola.security.admin.vo.ColaUserKycVO;
import com.bitcola.security.admin.vo.UserAddress;
import com.bitcola.security.common.msg.AppResponse;
import com.bitcola.security.common.msg.TableResultResponse;
import com.bitcola.security.common.util.AdminQuery;
import com.bitcola.security.common.util.Query;
import com.bitcola.entity.ColaUserKyc;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.RoundingMode;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author wx
 * @create 2018-11-27 12:06
 **/
@Service
public class ColaUserBiz {
    @Autowired
    ColaUserMapper mapper;

    @Autowired
    ColaKycMapper kycMapper;

    @Autowired
    UserMapper userMapper;

    @Autowired
    IActivityFeign activityFeign;


    public TableResultResponse<SysUserEntity> selectByQuery(AdminQuery query) {
        List<SysUserEntity> list = mapper.list(query);
        Long total = mapper.total(query);
        return new TableResultResponse<>(total, list);
    }

    public Map<String, Object> one(String id) {
        Map<String, Object> info = mapper.info(id);
        List<Map<String, Object>> balance = mapper.balance(id);
        List<Map<String, Object>> transaction = mapper.transaction(id);
        Map<String, Object> map = new HashMap<>();
        map.put("info", info);
        map.put("balance", balance);
        map.put("transaction", transaction);
        return map;
    }

    public TableResultResponse<ColaUserKycVO> kycList(Query query) {
        String name = query.get("name").toString();
        String kycStatus = query.get("kycStatus").toString();
//        pagerhelp分页
        PageHelper.startPage(query.getPage(), query.getLimit());
        List<ColaUserKycVO> colaUserKycs = kycMapper.kycList(name, kycStatus);
        PageInfo<ColaUserKycVO> pageInfo = new PageInfo<>(colaUserKycs);
        return new TableResultResponse<>(pageInfo.getTotal(), pageInfo.getList());
    }

    @Transactional
    public void auditKyc(String userId, String status, String reason) {
        ColaUserKyc colaUserKyc = kycMapper.selectByPrimaryKey(userId);
        colaUserKyc.setKycStatus(status);
        colaUserKyc.setReason(reason);
        kycMapper.updateByPrimaryKeySelective(colaUserKyc);
//      修改base_user信息
        userMapper.updateKycState(userId, status);
        Map<String, Object> map = userMapper.selectById(userId);
//       kafka

//        // 审核通过,发奖励
//        if (status == 1){
//            activityFeign.kycReward(userId);
//        }
    }

    @Transactional
    public AppResponse makePartner(String id) {
        ColaUserKyc colaUserKyc = kycMapper.selectByPrimaryKey(id);
        colaUserKyc.setKycStatus("LV2_PASS");
        kycMapper.updateByPrimaryKeySelective(colaUserKyc);
        userMapper.updateKycState(id, "LV2_PASS");
        return AppResponse.ok();
    }

    public Map<String, Object> kycDetail(String userId) {
        return kycMapper.kycDetail(userId);
    }

    public void update(String id, String username, String pin, Integer enable, String email, String telephone, String areaCode) {
        if (StringUtils.isNotBlank(username) || StringUtils.isNotBlank(email) || StringUtils.isNotBlank(telephone) || StringUtils.isNotBlank(areaCode)) {
            mapper.updateBaseUser(id, username, email, telephone, areaCode);
        }
        if ("0".equals(pin)) {
            mapper.resetPin(id);
        }
        if (enable != null) {
            mapper.updateColaUser(id, enable);
        }
    }

    public Integer repeat(String username, String email, String areaCode, String telephone) {
        return mapper.repeat(username, email, areaCode, telephone);
    }

    public TableResultResponse inviterList(AdminQuery query) {
        List<SysUserEntity> list = mapper.inviterList(query);
        Long total = mapper.inviterCount(query);
        return new TableResultResponse<>(total, list);
    }

    public TableResultResponse userAddressList(AdminQuery query) {
        List<UserAddress> list = mapper.userAddressList(query);
        for (UserAddress userAddress : list) {
            userAddress.setAvailable(userAddress.getAvailable().setScale(5, RoundingMode.HALF_UP));
            userAddress.setFrozen(userAddress.getFrozen().setScale(5, RoundingMode.HALF_UP));
        }
        Long total = mapper.userAddressCount(query);
        return new TableResultResponse<>(total, list);
    }

}
